package com.zybzzz.workapp.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zybzzz.workapp.R;
import com.zybzzz.workapp.entry.PlanDate;
import com.zybzzz.workapp.entry.SinglePlanDateLab;

import java.util.List;

/**
 * Project Name：workapp
 * Class Name：PlanDateFragment
 * description：容纳当天任务点的fragment
 * Date：2020-10-24 08:40
 * author：zhangyibo
 */
public class PlanDateFragment extends Fragment {

    private RecyclerView planDateRecyclerView;
    private PlanDateAdapter adapter;
    private String dateMsgString;

    private static final String DATEMSG_STRING="datemsg_string";

    public static PlanDateFragment makeNewInstance(String dateString){
        Bundle args=new Bundle();
        args.putSerializable(DATEMSG_STRING,dateString);
        PlanDateFragment planDateFragment=new PlanDateFragment();
        planDateFragment.setArguments(args);
        return planDateFragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dateMsgString=(String)getArguments().getSerializable(DATEMSG_STRING);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_dateplan_list,container,false);
        planDateRecyclerView=(RecyclerView)view.findViewById(R.id.dateplan_recycler_view);
        planDateRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    private class PlanDateHolder extends RecyclerView.ViewHolder{
        private PlanDate planDate;
        private TextView taskTextView;
        private TextView taskStatusTextView;
        private TextView taskTimeTextView;

        public PlanDateHolder(LayoutInflater inflater,ViewGroup parent){
            super(inflater.inflate(R.layout.date_plan_item,parent,false));

            taskTextView=(TextView)itemView.findViewById(R.id.planTaskTextView);
            taskStatusTextView=(TextView)itemView.findViewById(R.id.planTaskStatusTextView);
            taskTimeTextView=(TextView)itemView.findViewById(R.id.planTimeTextView);
        }

        public void bind(PlanDate planDate){
            this.planDate=planDate;
            taskTimeTextView.setText(this.planDate.getTaskText());
            taskStatusTextView.setText(this.planDate.getTaskStatus()? "已完成":"未完成");
            taskTextView.setText(this.planDate.getTaskTime());
        }
    }


    private class PlanDateAdapter extends RecyclerView.Adapter<PlanDateHolder>{
        private List<PlanDate> planDates;
        public PlanDateAdapter(List<PlanDate> planDates){
            this.planDates=planDates;
        }

        @NonNull
        @Override
        public PlanDateHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            //此处可能出错
            LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
            return new PlanDateHolder(layoutInflater,parent);
        }

        @Override
        public void onBindViewHolder(@NonNull PlanDateHolder holder, int position) {
            PlanDate planDate=planDates.get(position);
            holder.bind(planDate);
        }

        @Override
        public int getItemCount() {
            return planDates.size();
        }
    }

    private void updateUI(){
        SinglePlanDateLab planDateLab=SinglePlanDateLab.get();
        List<PlanDate> planDates=planDateLab.get().filterPlanByDate(dateMsgString);

        adapter=new PlanDateAdapter(planDates);
        planDateRecyclerView.setAdapter(adapter);
    }
}
